<?php

namespace App\Http\Controllers\Commit;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Commit\DictConfigRepository;
use Illuminate\Http\Request;

/**
 * @OA\Tag(
 *     name="后台菜单模块",
 *     description="菜单模块",
 * )
 */
class DictConfigController extends BaseController
{
    public function __construct(DictConfigRepository $DictDatarRepository)
    {
        $this->repoist = $DictDatarRepository;
    }

    public function index(Request $Request, $type)
    {
        //字段的获取
        return $this->repoist->oneSel($type);
    }

    public function getData(Request $request)
    {
        $keys = $request->get('keys');
        if (empty($keys)) {
            return $this->returnMsg(100, '请指明字段');
        }
        $where = [];
        $where['eq']['shop_id'] = $request->user->shop_id ?? 0;
        if (is_array($keys)) {
            $where['in']['keys'] = $keys;
        } else {
            $where['eq']['keys'] = $keys;
        }
        return $this->repoist->getDataByKeys($where);
    }

    public function add(Request $request)
    {
        $keys = $request->get('keys');
        if (empty($keys)) {
            return $this->returnMsg(100, '请指明字段');
        }
//        加载数据
        if ($request->has('loadData') && $request->get('loadData') >= 1) {
            $data = $this->getData($request);
//            有文本内容的时候要编码后给前端
            if ($request->get('loadData') == 2) {
                foreach ($data as $k => $v) {
                    if ($v['type'] == 0) {
                        $v['value'] = rawurlencode(html_entity_decode($v['value']));
                    }
                    $data[$k] = $v;
                }
            }
            return $this->returnMsg(200, '字段数据已返回', $data);
        }
        return $this->repoist->add($request->all());
    }
}
